#include "widget.h"
#include "ui_widget.h"
#include <QString>
#include<QIcon>
#include<QNetworkDatagram>
static const QString ip = "106.54.43.197";
static const quint16 port = 8888;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("微信");
    QIcon icon(":/wechat.png");
    this->setWindowIcon(icon);
    udpClient = new QUdpSocket(this);
    connect(udpClient,&QUdpSocket::readyRead,this,[=]()
    {
        QNetworkDatagram info = udpClient->receiveDatagram();
        QString recvMsg = info.data();//QString的构造函数可以直接转换!

        ui->listWidget_2->addItem("[服务器消息]: "+recvMsg);
    });
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_3_clicked()
{
    QString msg = ui->lineEdit->text();
    QNetworkDatagram info(msg.toUtf8(),QHostAddress(ip),port);
    udpClient->writeDatagram(info);
    ui->listWidget_2->addItem("[客户端消息]:" + msg);
    ui->lineEdit->clear();
}
